package org.example;

import java.util.*;


////////////////////
// Bellman Ford
////////////////////

//class Edge {
//    public int from;
//    public int to;
//    public int cost;
//    public Edge(int from, int to, int cost) {
//        this.from = from;
//        this.to = to;
//        this.cost = cost;
//    }
//}
//
//public class Main {
//    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int N = scanner.nextInt(), M = scanner.nextInt();
//
//        List<Edge> edges = new ArrayList<>();
//        for (int i = 0; i < M; i++) {
//            int s = scanner.nextInt();
//            int t = scanner.nextInt();
//            int v = scanner.nextInt();
//            edges.add(new Edge(s, t, v));
//        }
//
//        int[] minDist = new int[N + 1];
//        Arrays.fill(minDist, Integer.MAX_VALUE);
//        minDist[1] = 0;
//
//        for (int i = 1; i < N; i++) {
//            for (Edge e : edges) {
//                if (minDist[e.from] < Integer.MAX_VALUE && minDist[e.from] + e.cost < minDist[e.to]) {
//                    minDist[e.to] = minDist[e.from] + e.cost;
//                }
//            }
//        }
//
//        if (minDist[N] < Integer.MAX_VALUE) {
//            System.out.println(minDist[N]);
//        } else {
//            System.out.println("unconnected");
//        }
//    }
//}


////////////////////
// SPFA（Bellman Ford队列优化）
////////////////////

class Edge {
    public int to;
    public int cost;
    public Edge(int to, int cost) {
        this.to = to;
        this.cost = cost;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt(), M = scanner.nextInt();

        List<List<Edge>> grid = new ArrayList<>(N + 1);
        for (int i = 0; i <= N; i++) {
            grid.add(new ArrayList<>());
        }
        for (int i = 0; i < M; i++) {
            int s = scanner.nextInt();
            int t = scanner.nextInt();
            int v = scanner.nextInt();
            grid.get(s).add(new Edge(t, v));
        }

        int[] minDist = new int[N + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);

        Queue<Integer> queue = new ArrayDeque<>();

        minDist[1] = 0;
        queue.offer(1);

        while (!queue.isEmpty()) {
            int cur = queue.poll();
            for (Edge e : grid.get(cur)) {
                if (minDist[cur] + e.cost < minDist[e.to]) {
                    minDist[e.to] = minDist[cur] + e.cost;
                    queue.offer(e.to);
                }
            }
        }

        if (minDist[N] < Integer.MAX_VALUE) {
            System.out.println(minDist[N]);
        } else {
            System.out.println("unconnected");
        }
    }
}